/*
 * Copyright 2011-2020 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0.
 * See `LICENSE` in the project root for license information.
 */

package me.ijleex.mgmt.system.user.action;

import java.util.List;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import me.ijleex.mgmt.framework.entity.Result;
import me.ijleex.mgmt.framework.page.Page;
import me.ijleex.mgmt.framework.spring.ControllerSupport;
import me.ijleex.mgmt.system.user.entity.User;
import me.ijleex.mgmt.system.user.entity.UserRole;
import me.ijleex.mgmt.system.user.service.IUserRoleService;
import me.ijleex.mgmt.system.user.service.IUserService;

/**
 * 用户 信息操作处理
 *
 * @author liym
 * @since 2019-03-14 11:03:53 By 'codegen'
 */
@RequestMapping("/system/user")
@Controller
public class UserAction extends ControllerSupport<User> {

    /**
     * HTML 路径前缀
     */
    private static final String PATH_PREFIX = "system/user";

    private final IUserService service;
    private final IUserRoleService userRoleService;

    public UserAction(IUserService service, IUserRoleService userRoleService) {
        this.service = service;
        this.userRoleService = userRoleService;
    }

    @GetMapping("/")
    public String index() {
        return PATH_PREFIX + "/index";
    }

    /**
     * 新增 用户
     */
    @GetMapping("/add")
    public String add() {
        return PATH_PREFIX + "/add";
    }

    /**
     * 新增 用户
     */
    @PostMapping("/add")
    @ResponseBody
    public Result add(User user) {
        int x = this.service.insertUser(user);
        return Result.of(x);
    }

    /**
     * 删除 用户
     */
    @PostMapping("/remove")
    @ResponseBody
    public Result remove(String ids) {
        int x = this.service.deleteUserByIds(ids);
        return Result.of(x);
    }

    /**
     * 修改 用户
     */
    @GetMapping("/update/{id}")
    public String update(@PathVariable("id") Long id, ModelMap mmap) {
        User user = this.service.getUserById(id);
        mmap.put("user", user);
        return PATH_PREFIX + "/update";
    }

    /**
     * 修改 用户
     */
    @PostMapping("/update")
    @ResponseBody
    public Result update(User user) {
        int x = this.service.updateUser(user);
        return Result.of(x);
    }

    /**
     * 查询 用户列表
     */
    @GetMapping("/list")
    @ResponseBody
    public Page<User> getUserList(HttpServletRequest request, User user) {
        this.setPageParams(request, user);
        List<User> list = this.service.getUserList(user);
        return this.wrapPageData(list, user);
    }

    /**
     * 查询 用户信息
     * <p>
     * http://localhost:8082/ij-mgmt/system/user/getById?id=577054734542819328
     *
     * @param request 请求
     * @param id ID
     * @return 用户信息
     * @since 2019-03-25 11:17
     */
    @GetMapping("/getById")
    @ResponseBody
    public Result getUserById(HttpServletRequest request, Long id) {
        User user = this.service.getUserById(id);
        return user == null ? Result.error("用户不存在：id = " + id) : Result.ok(user);
    }

    // ===

    /**
     * 得到已授权给指定用户的角色的 id 列表
     *
     * <p>http://localhost:8082/ij-mgmt/system/user/roleIds?userId=577054734542819328</p>
     *
     * @param request 请求
     * @param userId 用户 ID
     * @return 角色 ID 列表
     */
    @GetMapping("/roleIds")
    @ResponseBody
    public List<Long> getRoleIdByUserId(HttpServletRequest request, Long userId) {
        return this.userRoleService.getRoleIdByUserId(userId);
    }

    /**
     * 用户授权
     *
     * <p>建立用户与角色的关联关系</p>
     *
     * <p>curl -d "userId=577054734542819328&roleIds=111111,222222" http://localhost:8082/ij-mgmt/system/user/grant</p>
     *
     * @param request 请求
     * @param object 用户、角色关系信息，必须包含 userId、roleIds 等
     * @return 返回信息
     */
    @PostMapping("/grant")
    @ResponseBody
    public Result grant(HttpServletRequest request, UserRole object) {
        object.setCreator("me");
        return this.userRoleService.grant(object);
    }

}
